import UserApi from "@/api/user"
import {ss} from '@/utils/storage'

const state = {
    userInfo: ss.get(process.env.VUE_APP_APPKEY_USER) || '',
    token: ''
}

// getters
const getters = {
    userInfo: state => state.userInfo,
    token: state => state.token
}

// actions
const actions = {
    //用户登录
    userLogin({commit}, params) {
        return new Promise((resolve, reject) => {
            UserApi.login(params).then(({data}) => {
                commit('SET_USER', data);
                    resolve(data);
            }).catch((error) => {
                reject(error)
            });
        })
    },
    //退出登录
    userLogout({commit}) {
        return new Promise((resolve, reject) => {
            // 判断当前的 href
            var pathname = window.location.pathname;
            //
            commit('SET_USER', '');//清空用户信息
            ss.remove(process.env.VUE_APP_APPKEY_USER);
            resolve()
        });
    },
    // //修改密码
    // changePassword({commit}, {oldPassword, newPassword}) {
    //     return new Promise((resolve, reject) => {
    //         UserLoginApi.changePassword({
    //             oldPassword: oldPassword,
    //             newPassword: newPassword
    //         }).then(({data}) => {
    //             resolve(data);
    //         }).catch((error) => {
    //             reject(error)
    //         });
    //     })
    // }
}

// mutations
const mutations = {
    SET_USER(state, userInfo) {
        ss.set(process.env.VUE_APP_APPKEY_USER, userInfo)
        state.userInfo = userInfo
    },
}

export default {
    state,
    getters,
    actions,
    mutations
}
